package com.water.devtool.ui.define;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.water.devtool.define.EntityBean;
import com.water.devtool.define.LevelConfig;
import com.water.devtool.service.CodeGenerateService;
import com.water.devtool.ui.CodePreviewDialog;
import com.water.devtool.ui.UiUtil;
import com.watermud.util.StringUtil;

public class EntityComposite extends Composite implements TabComposite {
	private final static Logger log = LoggerFactory.getLogger(EntityComposite.class);

	private CodeGenerateService service = new CodeGenerateService();

	private EntityBean entity;
	private Text text_desc;
	private Text text_code;
	private Text text_seqname;
	private Text text_java;
	private Text text_prefix;
	private Text text_pk;

	private Text text_datasource;
	private Button button_auto;
	private Button button_immutable;
	private Button button_logicDelete;

	private static Rectangle[] rects = new Rectangle[] { new Rectangle(591, 285, 121, 20),
			new Rectangle(747, 285, 121, 20), new Rectangle(882, 285, 121, 20), new Rectangle(591, 336, 121, 20),
			new Rectangle(747, 336, 121, 20), new Rectangle(882, 336, 121, 20), new Rectangle(591, 387, 121, 20),
			new Rectangle(747, 387, 121, 20), new Rectangle(882, 387, 121, 20) };
	private Text text_order;
	private Text text_deleteField;
	private Text text_deleteValue;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public EntityComposite(Composite parent, int style) {
		super(parent, style);

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (StringUtils.isBlank(text_java.getText())) {
					MessageDialog.openInformation(parent.getShell(), "提示", "请输入java类名!");
					return;
				}
				setEntity();
				CodePreviewDialog dialog = new CodePreviewDialog(getShell(), SWT.DIALOG_TRIM);
				dialog.setEntity(entity);
				dialog.open();
			}
		});
		button.setText("预览");
		button.setBounds(314, 489, 98, 30);

		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				closeTab();
			}
		});
		button_1.setText("取消");
		button_1.setBounds(649, 489, 98, 30);

		text_desc = new Text(this, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		text_desc.setBounds(167, 285, 352, 117);

		Label label = new Label(this, SWT.NONE);
		label.setText("名称：");
		label.setAlignment(SWT.RIGHT);
		label.setBounds(64, 56, 98, 20);

		text_code = new Text(this, SWT.BORDER);
		text_code.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == 13) { // 回车事件
					String pre = text_prefix.getText();
					String code = text_code.getText();
					log.debug("代码回车事件，code：{}，pre:{}", code, pre);
					if (StringUtils.isBlank(pre) && StringUtils.isNotBlank(code)) {
						int index = code.indexOf("_");
						text_prefix.setText(code.substring(0, index + 1));
					}
					setJavaName();
				}
			}
		});
		text_code.setBounds(167, 53, 349, 26);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setText("描述：");
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(86, 285, 76, 20);

		Label label_2 = new Label(this, SWT.NONE);
		label_2.setText("序列：");
		label_2.setAlignment(SWT.RIGHT);
		label_2.setBounds(67, 202, 98, 20);

		text_seqname = new Text(this, SWT.BORDER);
		text_seqname.setBounds(167, 199, 349, 26);

		Label lblJava = new Label(this, SWT.NONE);
		lblJava.setText("JAVA：");
		lblJava.setAlignment(SWT.RIGHT);
		lblJava.setBounds(64, 110, 98, 20);

		text_java = new Text(this, SWT.BORDER);
		text_java.setBounds(167, 107, 314, 26);

		Label label_3 = new Label(this, SWT.NONE);
		label_3.setText("前缀：");
		label_3.setAlignment(SWT.RIGHT);
		label_3.setBounds(551, 56, 98, 20);

		text_prefix = new Text(this, SWT.BORDER);
		text_prefix.setBounds(654, 53, 349, 26);

		Label label_4 = new Label(this, SWT.NONE);
		label_4.setText("主键：");
		label_4.setAlignment(SWT.RIGHT);
		label_4.setBounds(64, 156, 98, 20);

		text_pk = new Text(this, SWT.BORDER);
		text_pk.setBounds(167, 153, 349, 26);

		button_auto = new Button(this, SWT.CHECK);
		button_auto.setText("自增长");
		button_auto.setBounds(591, 202, 121, 20);

		Button button_3 = new Button(this, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (button_immutable.getSelection()) {
					MessageDialog.openInformation(parent.getShell(), "提示", "已经关闭，不能再生成文件!");
					return;
				}
				if (StringUtils.isBlank(text_java.getText())) {
					MessageDialog.openInformation(parent.getShell(), "提示", "请输入java类名!");
					return;
				}
				setEntity();
				createClass();
			}
		});
		button_3.setText("生成");
		button_3.setBounds(492, 489, 98, 30);

		Button button_4 = new Button(this, SWT.NONE);
		button_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setJavaName();
			}

		});
		button_4.setText("=");
		button_4.setBounds(493, 107, 26, 26);

		Label label_6 = new Label(this, SWT.NONE);
		label_6.setText("数据源：");
		label_6.setAlignment(SWT.RIGHT);
		label_6.setBounds(551, 153, 98, 20);

		text_datasource = new Text(this, SWT.BORDER);
		text_datasource.setBounds(654, 150, 349, 26);

		Label label_5 = new Label(this, SWT.NONE);
		label_5.setText("查询排序：");
		label_5.setAlignment(SWT.RIGHT);
		label_5.setBounds(70, 442, 98, 20);

		text_order = new Text(this, SWT.BORDER);
		text_order.setBounds(170, 439, 509, 26);

		button_immutable = new Button(this, SWT.CHECK);
		button_immutable.setText("关闭");
		button_immutable.setBounds(761, 442, 121, 20);

		button_logicDelete = new Button(this, SWT.CHECK);
		button_logicDelete.setText("逻辑删除");
		button_logicDelete.setBounds(591, 110, 106, 20);

		text_deleteField = new Text(this, SWT.BORDER);
		text_deleteField.setBounds(710, 107, 121, 26);

		text_deleteValue = new Text(this, SWT.BORDER);
		text_deleteValue.setBounds(897, 107, 106, 26);

		Label label_7 = new Label(this, SWT.NONE);
		label_7.setText("=");
		label_7.setAlignment(SWT.CENTER);
		label_7.setBounds(837, 110, 45, 20);

	}

	private void setJavaName() {
		if(StringUtils.isNotBlank(text_java.getText())) {
			return ;
		}
		String code = text_code.getText();
		if (StringUtils.isBlank(code)) {
			return;
		}

		code = code.toUpperCase();
		String pre = text_prefix.getText();
		if (StringUtils.isNotBlank(pre)) {
			if (code.startsWith(pre.toUpperCase())) {
				code = code.substring(pre.length());
			}
		}
		String javaName = StringUtil.capitalize(StringUtil.getJavaStyleName(code));
		log.debug("表名是：{}，前缀是：{},java名是：{}", new String[] { code, pre, javaName });
		text_java.setText(javaName);
	}

	protected void setEntity() {
		entity.setCode(text_code.getText());
		entity.setDesc(text_desc.getText());
		entity.setSeqName(text_seqname.getText());
		entity.setJavaName(text_java.getText());
		entity.setPrefix(text_prefix.getText());
		entity.setPrimaryKey(text_pk.getText());
		entity.setAutoIncrement(button_auto.getSelection());
		entity.setOrderSql(text_order.getText());
		entity.setClose(button_immutable.getSelection());
		entity.setLogicDelete(button_logicDelete.getSelection());
		entity.setDeleteField(text_deleteField.getText());
		entity.setDeleteValue(text_deleteValue.getText());
	}

	protected void createClass() {
		try {
			service.generate(entity);
			MessageDialog.openInformation(getShell(), "提示", "代码生成成功！");
		} catch (Exception e) {
			MessageDialog.openInformation(getShell(), "错误", e.getMessage());
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public EntityBean getEntity() {
		return entity;
	}

	public void setEntity(EntityBean entity) {
		if (this.entity != null) {
			return;
		}
		this.entity = entity;

		UiUtil.setText(text_code, entity.getCode());
		UiUtil.setText(text_order, entity.getOrderSql());
		UiUtil.setText(text_desc, entity.getDesc());
		UiUtil.setText(text_seqname, entity.getSeqName());
		UiUtil.setText(text_java, entity.getJavaName());
		UiUtil.setText(text_prefix, entity.getPrefix());
		UiUtil.setText(text_pk, entity.getPrimaryKey());
		UiUtil.setText(text_datasource, entity.getDataSource());
		UiUtil.setText(text_deleteField, entity.getDeleteField());
		UiUtil.setText(text_deleteValue, entity.getDeleteValue());
		button_auto.setSelection(entity.isAutoIncrement());
		button_immutable.setSelection(entity.isClose());
		button_logicDelete.setSelection(entity.isLogicDelete());

		List<LevelConfig> list = entity.getProject().listLevelConfig();
		int i = 0;
		for (LevelConfig levelConfig : list) {
			Button btnBean = new Button(this, SWT.CHECK);
			btnBean.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					if (btnBean.getSelection()) {
						entity.addClassConfig(levelConfig.getLevel());
					} else {
						entity.removeClassConfig(levelConfig.getLevel());
					}
				}
			});
			btnBean.setText(levelConfig.getLevel());
			if (entity.containClass(levelConfig.getLevel())) {
				btnBean.setSelection(true);
			}
			btnBean.setBounds(rects[i++]);

		}

	}

	public void closeTab() {
		TabFolder tabFolder = (TabFolder) getParent();
		TabItem[] items = tabFolder.getSelection();
		for (TabItem tabItem : items) {
			tabItem.dispose();
		}
	}
}
